package com.zyf.servicehosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyf.commomutil.result.Result;
import com.zyf.servicehosp.service.HospitalSetService;
import com.zyf.serviceutil.utils.MD5;
import com.zyf.yygh.model.hosp.HospitalSet;
import com.zyf.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;
    @ApiOperation(value = "查询所以医院信息")
    @GetMapping("/getAll")
    public Result getAllHospitalSet(){
        return Result.ok(hospitalSetService.list());
    }


    @ApiOperation(value = "根据id删除医院信息")
    @DeleteMapping("/{id}")
    public Result removeById(@ApiParam(value = "医院id") @PathVariable Long id){
        boolean b = hospitalSetService.removeById(id);
        if(b){
            return Result.ok();
        }else return Result.fail();
    }

    @ApiOperation(value = "条件分页查询医院列表")
    @PostMapping("/page/{pageNo}/{pageSize}")
    private Result page(@PathVariable Long pageNo,
                        @PathVariable Long pageSize,
                        @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<HospitalSet> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if(!StringUtils.isEmpty(hoscode)){
            lambdaQueryWrapper.eq(HospitalSet::getHoscode,hoscode);
        }
        if(!StringUtils.isEmpty(hosname)){
            lambdaQueryWrapper.like(HospitalSet::getHosname,hosname);
        }
        return Result.ok(hospitalSetService.page(page,lambdaQueryWrapper));
    }

    @ApiOperation(value = "根据id查询医院信息")
    @GetMapping("/{id}")
    public Result getHospitalSetById(@PathVariable Long id){
        return Result.ok(hospitalSetService.getById(id));
    }

    @ApiOperation(value = "根据id修改医院信息")
    @PostMapping("/update")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean b = hospitalSetService.updateById(hospitalSet);
        if(b){
            return Result.ok();
        }else return Result.fail();
    }

    @ApiOperation(value = "添加医院信息")
    @PostMapping("/save")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置可用性为1（可用）
        hospitalSet.setStatus(1);
        //随机生成密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" +random.nextInt(10000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if(save){
            return Result.ok();
        }else return Result.fail();
    }

    @ApiOperation(value = "批量删除医院信息")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean b = hospitalSetService.removeByIds(idList);
        if(b){
            return Result.ok();
        }else return Result.fail();
    }

    @ApiOperation(value = "修改医院设置状态(锁定和解锁)")
    @PutMapping("/changeStatus/{id}/{status}")
    public Result changeStatus(@PathVariable Long id,@PathVariable Integer status){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation(value = "发送签名密钥")
    @PutMapping("/sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hosname = hospitalSet.getHosname();
        //TODO 发送短信
        return Result.ok();
    }



}
